<?php
namespace app\xiangmu\controller;
class Xiangmu extends Base {
    //列表
    public function lists() {
        if (is_post()) {
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','title','like',"%{$d['keyword']}%"]; }
            if($d['min_time']!='') $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
            $w = where($map);
            $rs = $this->db->query("select * from xiangmu $w[0] order by sort desc,id desc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                foreach ($rs[0] as $key => $val) {
                  $rs[0][$key]['status'] = c("type.status")[$val['status']];
                  $rs[0][$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
                }
                suc($rs[0], $rs[1]);
            } else {
                suc([], 0);
            }
        }
        $this->fetch([],'','layadmin');
    }

    //添加
    public function add() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            unset($d['file']);
            $d['create_time'] = time();
            if(isset($d['content'])) $d['content']=htmlspecialchars_decode($d['content']);
            $id = $this->db->insert('xiangmu',$d);
            m('Attachment','xcom')->imgSure('xiangmu',$id,$d);
            cookie('js_msg', '添加成功');
            cookie('js_function', 'preload');
            suc();
        }
        $this->fetch([],'','layno');
    }

    //编辑
    public function edit() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            unset($d['file']);
            $d['update_time'] = time();
            if(isset($d['content'])) $d['content']=htmlspecialchars_decode($d['content']);
            $this->db->update('xiangmu',$d);
            m('Attachment','xcom')->imgSure('xiangmu',$d['id'],$d);
            cookie('js_msg', '修改成功');
            cookie('js_function', 'preload');
            suc();
        }
        $id = $this->get['id'];
        $info = $this->db->query("select * from xiangmu where id=?", [$id], 1);
        $this->fetch(['info' => $info],'','layno');
    }

    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('xiangmu',$d['id']);
            m('Attachment','xcom')->imgDel('xiangmu',$d['id'],true);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }
}
